package com.dfk.bs.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dfk.bs.common.constants.ResultCodeEnum;
import com.dfk.bs.common.exception.BsException;
import com.dfk.bs.common.vo.R;
import com.dfk.bs.dao.HouseMapper;
import com.dfk.bs.dao.HousetypeMapper;
import com.dfk.bs.entity.Eleapp;
import com.dfk.bs.entity.House;
import com.dfk.bs.entity.Housetype;
import com.dfk.bs.service.EleappService;
import com.dfk.bs.service.HouseService;
import com.dfk.bs.service.HousetypeService;
import com.dfk.bs.service.RoomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 房屋表 前端控制器
 * </p>
 *
 * @author DDK
 * @since 2019-12-12
 */

@Api("房屋管理")
@CrossOrigin //跨域
@RestController
@RequestMapping("/ddkbishe/house")
public class HouseController {

    @Autowired
    private HouseService houseService;
    @Resource
    private HousetypeService housetypeService;

    @Autowired
    private RoomService roomService;

    @ApiOperation("所有房屋列表")
    @GetMapping("/list")
    public R list(){
        List<House> list = houseService.list(null);
        return R.ok()
                .message("查询房屋列表成功")
                .data("items",list);
    }

    @ApiOperation("新增房屋")
    @PostMapping("/save")
    public R save(
            @ApiParam(name="house",value = "房屋信息",required = true)
            @RequestBody House house){
        houseService.save(house);
        //新增房屋时  获取房屋类型 对应新增房间以及用电器
        String typename = house.getTypename();
        String housecode = house.getHousecode();

        houseService.addRoomAndEleapp(typename,housecode);
        return R.ok()
                .message("新增房屋成功");
    }

    @ApiOperation("根据房屋门号删除房屋")
    @DeleteMapping("/remove/{houseid}")
    public R removeById(
            @ApiParam(name = "houseid",value = "房屋id", required = true)
            @PathVariable String houseid){
        houseService.removeById(houseid);
        return R.ok()
                .message("删除房屋成功");
    }

    @ApiOperation("分页条件查询房屋列表")
    @PostMapping("/pageQuery")
    public R pageQuery(@RequestBody Map<String, Object> params){
        Integer page = (Integer) params.get("page");
        Integer limit = (Integer) params.get("limit");
        String selectname = (String) params.get("selectname");
        //业务逻辑错误，抛出自定义异常
        if(page <= 0 || limit <= 0){
            throw new BsException(ResultCodeEnum.PARAM_ERROR);
        }
        Page<House> pageParam=new Page<>(page,limit);
        /*已经把查询的每一页的信息放到 pageParam中*/
        houseService.pageQuery(pageParam,selectname);
        List<Housetype> housetypes = housetypeService.list();
        List<House> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok()
                .message("分页条件房屋列表成功")
                .data("total",total)
                .data("rows",records)
                .data("rows2",housetypes);
    }

    @ApiOperation(value = "根据ID修改房屋")
    @PutMapping("/update")
    public R updateById(
            @ApiParam(name = "house", value = "房屋对象", required = true)
            @RequestBody House house){
        houseService.updateById(house);
        return R.ok()
                .message("修改房屋成功");
    }

    @ApiOperation(value = "根据housecode查询typename")
    @PostMapping("/selectByCode/{housecode}")
    public R selectByName(
            @ApiParam(name = "housecode", value = "房屋门号", required = true)
            @PathVariable("housecode") String housecode){

        House one = houseService.getOne(
                Wrappers.<House>lambdaQuery()
                        .eq(House::getHousecode, housecode), false

        );

        return R.ok().message("门号查户型").data("house",one);
    }


}

